package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityDisturbBinding;
import com.apex.aolonfitdemo.dialog.NotDisturbDialog;
import com.apex.aolonfitdemo.dialog.SwitchDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.DonDisturbCallback;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABleNotDisturb;
import com.cyber.bridge.module.bean.NotDisturbModeEntity;
import com.cyber.bridge.module.listener.OnCyberQueryAndSetStateListener;

public class NoDisturbActivity extends AppCompatActivity {
    private NotDisturbDialog startDialog;
    private NotDisturbDialog endDialog;
    private WaitingDialog waitingDialog;
    private SwitchDialog switchDialog;
    NotDisturbModeEntity notDisturbModeEntity;
    private String tempState;
    private int tempStartHour, tempStartMinute, tempEndHour, tempEndMinute;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                notDisturbModeEntity = (NotDisturbModeEntity) msg.obj;
                if (notDisturbModeEntity == null) {
                    Toast.makeText(NoDisturbActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
                    return;
                }
                tempStartHour = notDisturbModeEntity.getStartTime() / 60;
                tempStartMinute = notDisturbModeEntity.getStartTime() % 60;
                tempEndHour = notDisturbModeEntity.getEndTime() / 60;
                tempEndMinute = notDisturbModeEntity.getEndTime() % 60;
                if (!notDisturbModeEntity.isEnabled()) {
                    activityDisturbBinding.restScreen.setText(getString(R.string.switch_state_close));
                    tempState = getString(R.string.switch_state_close);
                } else {
                    activityDisturbBinding.restScreen.setText(getString(R.string.switch_state_on));
                    tempState = getString(R.string.switch_state_on);
                }
                activityDisturbBinding.startTime.setText(tempStartHour + ":" + tempStartMinute);
                activityDisturbBinding.endTime.setText(tempEndHour + ":" + tempEndMinute);
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityDisturbBinding.restScreen.setText(tempState);
                activityDisturbBinding.startTime.setText(tempStartHour + ":" + tempStartMinute);
                activityDisturbBinding.endTime.setText(tempEndHour + ":" + tempEndMinute);
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(NoDisturbActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityDisturbBinding activityDisturbBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityDisturbBinding = ActivityDisturbBinding.inflate(getLayoutInflater());
        setContentView(activityDisturbBinding.getRoot());
        activityDisturbBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityDisturbBinding.tool);
        getSupportActionBar().setTitle(null);
        activityDisturbBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(NoDisturbActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().setOnNotDisturbModeListener(new OnCyberQueryAndSetStateListener<NotDisturbModeEntity>() {
            @Override
            public void queryConfig(NotDisturbModeEntity notDisturbModeEntity) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.obj = notDisturbModeEntity;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void setConfig(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }

            }
        });
        AolonFitManager.getInstance().queryNotDisturbMode();

        activityDisturbBinding.restScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (switchDialog == null) {
                    switchDialog = new SwitchDialog(NoDisturbActivity.this);
                    switchDialog.setSelectListener(new SwitchDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            tempState = sex;
                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(NoDisturbActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                AolonFitManager.getInstance().setNotDisturbMode(new NotDisturbModeEntity(true, tempStartHour * 60 + tempStartMinute, tempEndHour * 60 + tempEndMinute));

                            } else {
                                AolonFitManager.getInstance().setNotDisturbMode(new NotDisturbModeEntity(false, tempStartHour * 60 + tempStartMinute, tempEndHour * 60 + tempEndMinute));
                            }


                        }
                    });
                }
                if (!switchDialog.isShowing()) {
                    switchDialog.show();
                }

            }
        });
        activityDisturbBinding.startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (startDialog == null) {
                    startDialog = new NotDisturbDialog(NoDisturbActivity.this, getString(R.string.start_time));
                    startDialog.setSelectListener(new NotDisturbDialog.SelectListener() {
                        @Override
                        public void selectData(int hour, int minute) {

                            tempStartHour = hour;
                            tempStartMinute = minute;

                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(NoDisturbActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            if (getString(R.string.switch_state_on).equalsIgnoreCase(tempState)) {
                                AolonFitManager.getInstance().setNotDisturbMode(new NotDisturbModeEntity(true, tempStartHour * 60 + tempStartMinute, tempEndHour * 60 + tempEndMinute));

                            } else {
                                AolonFitManager.getInstance().setNotDisturbMode(new NotDisturbModeEntity(false, tempStartHour * 60 + tempStartMinute, tempEndHour * 60 + tempEndMinute));
                            }

                        }
                    });
                }
                if (!startDialog.isShowing()) {
                    startDialog.show();
                }

            }
        });
        activityDisturbBinding.endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (endDialog == null) {
                    endDialog = new NotDisturbDialog(NoDisturbActivity.this, getString(R.string.end_time));
                    endDialog.setSelectListener(new NotDisturbDialog.SelectListener() {
                        @Override
                        public void selectData(int hour, int minute) {

                            tempEndHour = hour;
                            tempEndMinute = minute;

                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(NoDisturbActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            if (getString(R.string.switch_state_on).equalsIgnoreCase(tempState)) {
                                AolonFitManager.getInstance().setNotDisturbMode(new NotDisturbModeEntity(true, tempStartHour * 60 + tempStartMinute, tempEndHour * 60 + tempEndMinute));

                            } else {
                                AolonFitManager.getInstance().setNotDisturbMode(new NotDisturbModeEntity(false, tempStartHour * 60 + tempStartMinute, tempEndHour * 60 + tempEndMinute));
                            }

                        }
                    });
                }
                if (!endDialog.isShowing()) {
                    endDialog.show();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (switchDialog != null) {
            switchDialog.dismiss();
            switchDialog.destroyDialog();
            switchDialog = null;
        }
        if (startDialog != null) {
            startDialog.dismiss();
            startDialog.destroyDialog();
            startDialog = null;
        }
        if (endDialog != null) {
            endDialog.dismiss();
            endDialog.destroyDialog();
            endDialog = null;
        }
        super.onDestroy();
    }
}
